package com.gooagoo.supercrm.ui.activity.manage;

import android.graphics.Color;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.gooagoo.supercrm.R;
import com.gooagoo.supercrm.base.BaseActivity;
import com.gooagoo.supercrm.ui.activity.login.ChangePwdActivity;
import com.gooagoo.supercrm.utils.SPUtils;
import com.gooagoo.supercrm.utils.ToastUtil;
import com.gooagoo.supercrm.widget.CustomDialog;
import com.tencent.tinker.lib.tinker.TinkerInstaller;

import java.io.File;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

public class ManageActivity extends BaseActivity implements View.OnClickListener {

    private CustomDialog customDialog;
    private EditText et_dlg_emali;
    private CustomDialog customDialog1;

    @OnClick(R.id.rl_change_email)
    void chageEmail(){
        customDialog = new CustomDialog(this, R.layout.dialog_input_email) {
            @Override
            public void initView() {
                findViewById(R.id.btn_dlg_emali_qr).setOnClickListener(ManageActivity.this);
                findViewById(R.id.btn_dlg_emali_qx).setOnClickListener(ManageActivity.this);
                et_dlg_emali = findViewById(R.id.et_dlg_emali);
            }
        };
    }
    @BindView(R.id.tv_canUpdata)
    TextView tv_canUpdata;
    @BindView(R.id.tv_version)
    TextView tv_version;

    @OnClick(R.id.rl_updata)
    void checkUpdata(){

            if ( new File( Environment.getExternalStorageDirectory().getAbsolutePath() + "/patch_signed_7zip.apk").exists()){
                TinkerInstaller.onReceiveUpgradePatch(getApplicationContext(), Environment.getExternalStorageDirectory().getAbsolutePath() + "/patch_signed_7zip.apk");
            }

        customDialog1 = new CustomDialog(mContext, R.layout.updating) {
            @Override
            public void initView() {

            }
        };
    }


    @OnClick(R.id.rl_change_pwd)
    void  changePwd(){
        startActivity(ChangePwdActivity.class,null);
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_mymanage;
    }
    int count = 0;
    @Override
    protected void init() {
        //请求网络 判断是否升级
//#FFEB4242

        Observable.interval(2000, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Long>() {
            @Override
            public void accept(Long aLong) throws Exception {
                count++;
                if (count%2==0){
                    tv_canUpdata.setText("可升级版本");
                    tv_canUpdata.setTextColor(Color.parseColor("#FFEB4242"));
                }else {
                    tv_canUpdata.setText("当前为最新版本");
                    tv_canUpdata.setTextColor(Color.parseColor("#FF666666"));
                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_dlg_emali_qx:
                customDialog.dismiss();
                break;

            case R.id.btn_dlg_emali_qr:
//                ^([a-z0-9A-Z]+[-|.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?.)+[a-zA-Z]{2,}$
                String email = et_dlg_emali.getText().toString().trim();
                String regx = new String("^[a-z0-9]+([._\\\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$");
                if(email.matches(regx)){
                    SPUtils.put(mContext,"email",email);
                    customDialog.dismiss();
                }else {
                    ToastUtil.showToast(mContext,"邮箱非法,请重新输入");
                }
                break;
        }
    }
}
